#!/bin/bash
# 
# Making boot sd card for batch production.
# 
#  Example usage:
#  create_sd_fs.sh [zedboard] [avc]
# 
version=1.1
fs_path="fs-1.5"
board=${1:-"zedboard"}
user=${2}
echo "version:$version"

function build_uenv()
{
    if [[ ! -n $1 ]];then
        return 1
    fi
    local path=${1}

    echo "uenvcmd=run avc_sdboot

serverip=192.168.50.4
avc_sdboot=echo Copying Linux from SD to RAM... && fatload mmc 0 0x3000000 \${kernel_image} && fatload mmc 0 0x2A00000 devicetree.dtb && if fatload mmc 0 0x2000000 \${ramdisk_image}; then bootm 0x3000000 0x2000000 0x2A00000; else bootm 0x3000000 - 0x2A00000; fi
tftp_boot=echo Download Linux from Srv &&  tftpboot 0x3000000 tong/update/arf9022/debug/uImage && tftpboot 0x2A00000 tong/update/arf9022/debug/devicetree.dtb && bootm 0x3000000 - 0x2A00000
tftp_boot2=echo Download Linux from Srv && tftpboot 0x3000000 tong/update/ad9361/uImage && tftpboot 0x2A00000 tong/update/zedboard/ad9361/devicetree.dtb && bootm 0x3000000 - 0x2A0000

#bootargs=console=ttyPS0,115200 root=/dev/mmcblk0p2 rw earlyprintk rootfstype=ext4 dyndbg=\"file cf_axi_adc_core.c +p; file spi.cs +p; file arf9022.c +p; file arf9022.c line 2702 -p; arf9022_noos.c +p; file industrialio-core.c +p; file cf_axi_dds.c +p; file arf9022_cov.c +p; file spi-avc.sc +p; file spi-cadence.cs +p, file clk.c +p\" loglevel=8 rootwait
bootargs=console=ttyPS0,115200 root=/dev/mmcblk0p2 rw earlyprintk rootfstype=ext4 rootwait" > $path/uEnv.txt

}

function user_name_get()
{
    local user=`cat /etc/passwd | grep bash | cut -f 1 -d :`
    for u in $user
    do
        if [[ $u != "root" ]];then
            echo $u
        fi
    done
}

if [ "$(id -u)" != "0" ]; then
   echo "This script must be run as root!" 1>&2
   exit 1
fi

if [[ ! -n $user ]];then
    user=`user_name_get`
fi

echo "Building the directory of $board and necessary files"
# 更新kernel files
su $user -c ./kernel_update.sh ./$board &>/dev/null
if [ $? -eq 2 ];then
    echo "Faild:Could not connect to server!"
    exit 1
fi
su $user -c ./bootpull.sh ./$board &>/dev/null

if [ ! -f "./$board/uEnv.txt" ];then
    echo "build uEnv.txt"
    build_uenv "./$board/"
fi

# 利用shell脚本实现对一个磁盘进行分区格式化挂载
PS3="提示选择要创建的磁盘："
select W in `ls /dev/sd*|grep -o 'sd[b-z]'|uniq` quit
do
        case $W in
        sda)
            fdisk -l /dev/sda
            break;;
        sd[b-z])
        
            echo -e "d\nd\nw\n" | fdisk /dev/$W &>/dev/null
            if [ $? -ne 0 ];then
                echo "Failed:Could not open /dev/$W: Media not found"
                exit 1
            fi
            echo -e "o\nn\np\n1\n\n+477M\nn\np\n2\n\n\nw\n" | fdisk /dev/$W
            bootpoint=`lsblk -P /dev/${W}1`
            bootpoint=${bootpoint##*"MOUNTPOINT=\""}
            bootpoint=${bootpoint%"\""*}

            if [[ -n $bootpoint ]];then
                umount /dev/${W}1
                echo "umount ${W}1"
            fi
            mkfs -t vfat /dev/${W}"1" 
            if [ $? -ne 0 ];then
                echo "Failed:Format error!"
                exit 1
            fi

            rootfspoint=`lsblk -P /dev/${W}2`
            rootfspoint=${rootfspoint##*"MOUNTPOINT=\""}
            rootfspoint=${rootfspoint%"\""*}

            if [[ -n $rootfspoint ]];then
                umount /dev/${W}2
                echo "umount ${W}2"
            fi
            
            echo -e "y\n" | mkfs -t ext4 /dev/${W}"2" &&
            if [ $? -ne 0 ];then
                echo "Failed:Format partition 2 error!"
                exit 1
            fi
            sleep 5
            echo "Change Partial Label"
            dosfslabel /dev/${W}"1" boot &>/dev/null
            e2label /dev/${W}"2" rootfs &>/dev/null
            mkdir -p /media/boot &>/dev/null
            mkdir -p /media/rootfs &>/dev/null

            mount /dev/${W}"1" /media/boot/ &>/dev/null &&
            if ! mountpoint -q /media/boot/;then
                echo "Faild:partition boot mount failed"
            fi
            mount /dev/${W}"2" /media/rootfs/ &>/dev/null &&
            if ! mountpoint -q /media/rootfs/;then
                echo "Faild:partition rootfs mount failed"
            fi
            sleep 3
            cp -rvp /home/avc/$fs_path/* /media/rootfs &&
            cp -rv /home/avc/$board/* /media/boot &&

            umount /media/boot &&
            umount /media/rootfs
            echo "启动SD卡制作完成, 可以移除SD卡了^v^"
            break;;
        quit)
            break;;
        *)
            echo "错误的磁盘，请再次检查";;
        esac
done
